import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateOrUpdateTaskInput, OrganizationUnitDto, PasswordComplexitySetting, ProfileServiceProxy, TaskForModelDto, TaskServiceProxy , OutsourcingDto} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData } from '../../shared/organization-unit-tree.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { TaskService } from '../../task/Task.service';

@Component({
  selector: 'createordinaryform',
  templateUrl: './create-ordinary-form.component.html',
  styleUrls: ['./create-ordinary-form.component.css']
})
export class CreateOrdinaryFormComponent extends AppComponentBase  {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;
  task: TaskForModelDto = new TaskForModelDto();
  outsourcing: OutsourcingDto = new OutsourcingDto();
  constructor(injector: Injector,
    private _taskServiceProxy: TaskServiceProxy,
    private _taskService: TaskService,
    private _profileService: ProfileServiceProxy) {
      super(injector);
    }

    show(taskmodel: TaskForModelDto): void {
      console.log(taskmodel);
      this.active = true;
      this.task = taskmodel;
      this.outsourcing = this._taskService.getOutSourcings().filter(x => x.taskForModelId === taskmodel.id)[0];
      console.log(this.outsourcing);
      console.log(this.outsourcing === undefined);
      if ( this.outsourcing === undefined) {
        this.outsourcing = new OutsourcingDto();
      }
      this.modal.show();
    }

    onShown(): void {
      //document.getElementById('Code').focus();
    }

    save(): void {
      this.outsourcing.unitPrice = 0;
      this.outsourcing.taskCount = this.task.amount;
      this.outsourcing.taskForModelId = this.task.id;
      this.outsourcing.picNo = this.task.picNo;
      this.outsourcing.taskName = this.task.displayName;
      this.outsourcing.taskCode = this.task.code;
      this._taskService.addToOutsourcs(this.outsourcing);
      this.notify.info(this.l('SavedSuccessfully'));
      this.close();
      this.modalSave.emit(null);
    }

    close(): void {
      this.active = false;
      this.modal.hide();
    }
}
